import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

export default class TeamProjectRequirementsListComponent extends Component {
  @tracked showModal = false;
  @tracked editingRequirement = null;
  @tracked formData = {
    name: '',
    productManager: '',
    status: '待评审',
    reviewTime: '',
    integrationTime: '',
    testTime: '',
    releaseTime: '',
    devHours: 0,
    testHours: 0,
    description: '',
    priority: '',
    m1Address: ''
  };

  // 状态对应的样式类
  getStatusClass = (status) => {
    const classes = {
      '待评审': 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300',
      '技术方案': 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300',
      '开发': 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300',
      '测试': 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300',
      '上线': 'bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-300'
    };
    return classes[status] || classes['待评审'];
  }

  // 根据成员名字获取对应的样式类
  getMemberTypeClass = (memberName) => {
    if (!memberName || !this.args?.members) {
      return 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300';
    }
    
    const member = this.args?.members?.find(m => m.name === memberName);
    if (!member) return 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300';
    
    const classes = {
      '前端': 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300',
      '后端': 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300',
      '测试': 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300'
    };
    
    return classes[member.type] || 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300';
  }

  // 去重开发人员列表
  getUniqueDevelopers = (developers) => {
    if (!Array.isArray(developers)) return [];
    
    const seen = new Set();
    return developers.filter(dev => {
      const name = typeof dev === 'object' ? dev.name : dev;
      if (seen.has(name)) {
        return false;
      }
      seen.add(name);
      return true;
    });
  }

  // 去重测试人员列表
  getUniqueTesters = (testers) => {
    if (!Array.isArray(testers)) return [];
    
    const seen = new Set();
    return testers.filter(tester => {
      const name = typeof tester === 'object' ? tester.name : tester;
      if (seen.has(name)) {
        return false;
      }
      seen.add(name);
      return true;
    });
  }


  @action
  showAddModal() {
    this.editingRequirement = null;
    this.formData = {
      name: '',
      productManager: '',
      status: '待评审',
      reviewTime: '',
      integrationTime: '',
      testTime: '',
      releaseTime: '',
      devHours: 0,
      testHours: 0,
      description: '',
      priority: '',
      m1Address: ''
    };
    this.showModal = true;
  }

  @action
  editRequirement(requirement) {
    this.editingRequirement = requirement;
    this.formData = { ...requirement };
    this.showModal = true;
  }

  @action
  hideModal() {
    this.showModal = false;
    this.editingRequirement = null;
  }

  @action
  updateFormData(field, event) {
    this.formData = {
      ...this.formData,
      [field]: event.target.value
    };
  }

  @action
  saveRequirement(event) {
    event.preventDefault();
    
    if (!this.formData.name.trim()) {
      // 在实际应用中应该显示错误消息
      return;
    }

    if (this.editingRequirement) {
      // 更新现有需求
      this.args.onUpdateRequirement?.(this.editingRequirement.id, this.formData);
    } else {
      // 新增需求
      this.args.onAddRequirement?.(this.formData);
    }

    this.hideModal();
  }

  @action
  deleteRequirement(requirementId) {
    if (confirm('确定要删除这个需求吗？')) {
      this.args.onDeleteRequirement?.(requirementId);
    }
  }
}